﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Video.VFW;
using AForge.Video.FFMPEG;

namespace WebCam
{
    public partial class Form1 : Form
    {
        private bool DeviceExist = false;
        private FilterInfoCollection videoDevices;
        private VideoCaptureDevice videoSource = null;
        Camera cam = null;

        public Form1()
        {
            InitializeComponent();
        }

        // get the devices name
        private void getCamList()
        {
            try
            {
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                comboBox1.Items.Clear();
                if (videoDevices.Count == 0)
                    throw new ApplicationException();

                DeviceExist = true;
                foreach (FilterInfo device in videoDevices)
                {
                    comboBox1.Items.Add(device.Name);
                    videoSource = new VideoCaptureDevice(device.MonikerString);
                    if (videoSource.VideoCapabilities != null)
                    {
                        foreach (VideoCapabilities vc in videoSource.VideoCapabilities)
                        {
                            comboBox2.Items.Add(vc.FrameSize.Width + "x" + vc.FrameSize.Height);
                        }
                        comboBox2.SelectedIndexChanged += new EventHandler(comboBox2_SelectedIndexChanged);
                    }
                }
                comboBox1.SelectedIndex = 0; //make dafault to first cam
            }
            catch (ApplicationException)
            {
                DeviceExist = false;
                comboBox1.Items.Add("No capture device on your system");
            }
        }

        void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int height = Convert.ToInt32(((string)comboBox2.SelectedItem).Split('x')[1]);
            int width = Convert.ToInt32(((string)comboBox2.SelectedItem).Split('x')[0]);
            pictureBox1.Width = width;
            pictureBox1.Height = height;
            if (cam != null) cam.CloseCam();
            Preview.Text = "Shutdown Camera";
            cam = new Camera(videoDevices[comboBox1.SelectedIndex].MonikerString, width, height, 25);
            cam.NewFrame += new EventHandler<NewFrameEventArgs>(cam_NewFrame);
            cam.Preview();
        }
        //refresh button
        private void rfsh_Click(object sender, EventArgs e)
        {
            getCamList();
        }

        //toggle start and stop button
        private void start_Click(object sender, EventArgs e)
        {
            if (start.Text == "Record")
            {
                if (DeviceExist)
                {
                    cam.CloseCam();
                    cam.Record(@"d:\test.avi");

                    start.Text = "Stop";
                }
                else
                {
                    label2.Text = "Error: No Device selected.";
                }
            }
            else
            {
                cam.StopRecording();
                start.Text = "Record";
                cam.Preview();
            }
        }

        void cam_NewFrame(object sender, NewFrameEventArgs e)
        {
            Bitmap img = (Bitmap)e.Frame.Clone();
            //do processing here
            pictureBox1.Image = img;
        }

        //eventhandler if new frame is ready
        private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            
            Bitmap img = (Bitmap)eventArgs.Frame.Clone();
            //do processing here
            AForge.Imaging.UnmanagedImage uImg = new AForge.Imaging.UnmanagedImage((System.Drawing.Imaging.BitmapData)eventArgs.Frame.Clone());

            uImg.CollectActivePixels();


            pictureBox1.Image = img;
        }

        //get total received frame at 1 second tick
        private void timer1_Tick(object sender, EventArgs e)
        {
            label2.Text = "Device running... " + videoSource.FramesReceived.ToString() + " FPS";
        }

        private void Preview_Click(object sender, EventArgs e)
        {
            if (Preview.Text == "Preview")
            {
                cam.Preview();
                Preview.Text = "Shutdown Camera";
            }
            else
            {
                cam.CloseCam();
                Preview.Text = "Preview";
            }
        }

        private void trackExposure_Scroll(object sender, EventArgs e)
        {
            txtExposure.Text = "Exposure - " + trackExposure.Value;
            cam.Exposure = trackExposure.Value;
        }
        private void trackFocus_Scroll(object sender, EventArgs e)
        {
            txtFocus.Text = "Focus - " + trackFocus.Value;
            cam.Focus = trackFocus.Value;
        }


    }
}
